<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 银行列表
 * Class Bank
 * @author  heyu
 */
class Bank extends MY_Controller {
    public $mBank;

	public function __construct(){
		parent::__construct();
		$this->loadModel('AdmBankModel', 'mBank', 'admin');
	}

	/**
	 * 首页
	 */
	public function index(){
		$data = array(
			'title' =>'银行列表'
		);
        $this->view('fund/bank',$data,'datatable,fileinput,validation,cropit_master');
	}

	/**
	 * 列表
	 */
	public function getList(){
		$data = $this->mBank->getAdminPageList();
		if(!empty($data['data'])){
			foreach ($data['data'] as $k =>$v){
                $data['aaData'][$k][] = $v['id'];
                $img = $v['bank_icon'] ? getImage($v['bank_icon'],'xs') : getDefaultImg();
				$data['aaData'][$k][] = '<img src="'.$img.'" class="img-wh">';
                $data['aaData'][$k][] = $v['bank_name'];
                $btnConfig = adminCreateBtn(array(
                    'label' => ' 编辑',
                    'icon' => '',
                    'attr' => array(
                        'href' => 'javascript:;',
                        'class' => 'edit-btn btn default btn-xs blue-stripe',
                        'data-id' => $v['id'],
                    ),
                    'tag' => 'a',
                    'permissions' => array(
                        'module' => 'fund',
                        'key' => 'bank-edit'
                    )
                ));
                $btnConfig .= ' '.adminCreateBtn(array(
                        'label' => ' 删除',
                        'icon' => '',
                        'attr' => array(
                            'href' => getUrl('fund/bank/deleteBank?id='.$v['id']),
                            'class' => 'ajax-request-btn btn default btn-xs red-stripe',
                            'alert-message' => "确认要删除".$v['bank_name']."吗？"
                        ),
                        'tag' => 'a',
                        'permissions' => array(
                            'module' => 'fund',
                            'key' => 'bank-delete'
                        )
                    ));
				$data['aaData'][$k][] = $btnConfig;
			}
		}

		unset($data['data']);
		echo json_encode($data);
	}

    /**
     * 保存信息
     */
	public function save(){
        //表单验证
        $vConf = array(
            array('bank_name', '银行卡名称', 'required'),
        );
        $this->formDataValidation($vConf);

        $id = (int)post('id');
        $bank_icon = trim(post('bank_icon'));

        //上传图片
        if(!$id && !$bank_icon){
            $this->ajaxReturn(0,' 请上传银行logo!');
        }

        //添加数据
        $tData = array(
            'bank_name'=>trim(post('bank_name')),
        );

        ##开启事物
        $this->db->trans_begin();   //开启事物
        if (!$id) {
            //保存数据
            $id = $this->mBank->save($tData);
            if (!$id) {
                $this->db->trans_rollback();
                $this->ajaxReturn(0, '保存失败');
            }
        }
        //上传
        if( $bank_icon ){
            $path = 'uploads/fund/bank/'.$id;

            $tA = saveCSVimg(array($bank_icon), $path, 'bank_icon');

            if( ! $tA){
                $this->db->trans_rollback();
                $this->appReturn(0, '图片上传失败');
            }
            $tData['bank_icon'] = $tA[0];
        }

        //更新图片地址
        $rS=$this->mBank->save($tData,$id);
        if (!$rS){
            $this->db->trans_rollback();
            $this->ajaxReturn(0,'保存失败');
        }
        $this->db->trans_commit(); //提交事物
        $this->ajaxReturn(1,'保存成功');
	}

    /**
     * 获取银行详情
     */
    public function getInfo(){
        //表单验证
        $vConf = array(
            array('id', 'id', 'required|is_natural_no_zero'),
        );
        $this->formDataValidation($vConf);
        $id = (int)post('id');
        $info = $this->mBank->getInfo($id);
        if (!$info){
            $this->ajaxReturn(0, '信息不存在');
        }
        $this->ajaxReturn(1, '', $info);
    }

    /**
     * 删除
     */
	public function deleteBank()
	{
		$id = (int)get('id');
		if(! $id){
            $this->ajaxReturn(0,'数据错误');
        }
		$tId=$this->mBank->deleteBank($id);
		if(! $tId){
			$this->ajaxReturn(0,'删除失败');
		}
		$this->ajaxReturn(1,'删除成功');
	}
	
}
